<?php



namespace App\Services;




use App\Models\SiteConfig;
use App\Models\StoreShop;
use Dcat\Admin\Admin;

class Data
{

    /**
     *
     * @param $key
     * @return mixed|null
     */
    public function getConfig($key)
    {
        return SiteConfig::where('key', $key)->value('value');
    }


    /**
     * @param string $str
     * @return string
     */
    public static function orderTimeNumber($str = '')
    {
        return $str . date('YmdHi') . rand(1000, 9999);
    }

    /**
     * 生成券碼
     * @return string
     */
    public function randomNumber()
    {
        return rand(10000000, 99999999);
    }

    /**
     * 字符串转数组
     * @param string $text 待转内容
     * @param string $separ 分隔字符
     * @param null|array $allow 限定规则
     * @return array
     */
    function str2arr(string $text, string $separ = ',', ?array $allow = null): array
    {
        $items = [];
        foreach (explode($separ, trim($text, $separ)) as $item) {
            if ($item !== '' && (!is_array($allow) || in_array($item, $allow))) {
                $items[] = $item;
            }
        }
        return $items;
    }

    /**
     * 数组转字符串
     * @param array $data 待转数组
     * @param string $separ 分隔字符
     * @param null|array $allow 限定规则
     * @return string
     */
    function arr2str(array $data, string $separ = ',', ?array $allow = null): string
    {
        foreach ($data as $key => $item) {
            if ($item === '' || (is_array($allow) && !in_array($item, $allow))) {
                unset($data[$key]);
            }
        }
        return $separ . join($separ, $data) . $separ;
    }


    /**
     * 格式化时间
     * @param $time
     * @return string|void
     */
    public function formatChatDate($time)
    {
        $timeDiff = now()->timestamp - $time;
        $f = [
            '86400' => '天',
            '3600' => '小时',
            '60' => '分钟',
            '1' => '秒'
        ];
        foreach ($f as $k => $v) {
            if (0 != $c = floor($timeDiff / (int)$k)) {
                if ($k == 86400 && $c > 3) {
                    return date('m-d H:i', $time);
                } else {
                    return $c . $v . '前';
                }
            }
        }
    }

    /**
     * 一维数据数组生成数据树
     * @param array $list 数据列表
     * @param string $id 父ID Key
     * @param string $pid ID Key
     * @param string $son 定义子数据Key
     * @return array
     */
    public function arrtotree($list, $id = 'id', $pid = 'pid', $son = 'sub')
    {
        list($tree, $map) = [[], []];
        foreach ($list as $item) $map[$item[$id]] = $item;
        foreach ($list as $item) if (isset($item[$pid]) && isset($map[$item[$pid]])) {
            $map[$item[$pid]][$son][] = &$map[$item[$id]];
        } else $tree[] = &$map[$item[$id]];
        unset($map);
        return $tree;
    }

    public static function getAdminCommonInfo(){
        $user=Admin::user();

        $port_type=0;
        $is_user=true;
        if($user->isAdministrator()){
            $port_type=1;
            $is_user=false;
        }else if($user->isRole(2)){
            $port_type=2;
        }else if($user->isRole(3)){
            $port_type=3;
        }
        $uid=$is_user?StoreShop::where('id',$user->shop_id)->value("uid"):0;
        $res=[
            'id'=>$user->id,
            'shop_id'=>$user->shop_id,
            'uid'=>$uid,
            'port_type'=>$port_type,
            'is_user'=>$is_user
        ];
        return $res;
    }
}
